﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ScrumTable.Common.Collections;
using ScrumTable.DL.Data.Generic.Cache;
using ScrumTable.DL.Data.Generic.Test.Mocks;

using WorkItemBucket = ScrumTable.DL.Data.Generic.Cache.CacheElementListEntryBucket<ScrumTable.DL.Data.Generic.Cache.CacheWorkItem>;
using WorkItemSavePair = ScrumTable.DL.Data.Generic.Cache.CacheSavePair<ScrumTable.DL.Data.Generic.Cache.CacheElementListEntryBucket<ScrumTable.DL.Data.Generic.Cache.CacheWorkItem>>;

#endregion

namespace ScrumTable.DL.Data.Generic.Test.Cache
{
    /// <summary>
    /// This is the test class for CacheWorkItemRevision class.
    /// </summary>
    [TestClass]
    public class CacheWorkItemRevisionTest : CacheIdentityBaseTest<CacheWorkItemRevision>
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        private CacheProject Project { get; set; }
        private CacheWorkItem UserStory { get; set; }
        #endregion

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new CacheWorkItemRevisionTest instance.
        /// </summary>
        public CacheWorkItemRevisionTest()
        {
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Use TestInitialize to run code before running each test .
        /// </summary>
        [TestInitialize]
        public override void TestInitialize()
        {
            base.TestInitialize();

            Project = CacheContext.Projects.TryGetById(Resources.Project1.Id);
            UserStory = Project.WorkItems.TryGetById(ResourceManager.UserStory1Id);
            ToTest = UserStory.Revisions.First();
        }

        /// <summary>
        /// Use TestCleanup to run code after each test has run.
        /// </summary>
        [TestCleanup]
        public override void TestCleanup()
        {
            ToTest = null;
            base.TestCleanup();
        }

        [TestMethod]
        public void TestRevisionsAvailable()
        {
            Assert.AreEqual(ResourceManager.Project1UserStory1RevisionCount, UserStory.Revisions.Count);
        }

        [TestMethod]
        public void TestNewRevisionsFromServer()
        {
            TestNewRevisionsFromServer(CacheSavePairState.Saved);
        }

        [TestMethod]
        public void TestNewRevisionsFromServerWithUpdate()
        {
            TestNewRevisionsFromServer(CacheSavePairState.Updated);
        }

        private void TestNewRevisionsFromServer(CacheSavePairState updateState)
        {
            IList<CacheWorkItemRevision> revisions = UserStory.Revisions.CopyToList();
            
            // touch story, so it must be updated
            UserStory.Name = "[A New Name]";

            // special case: in mean time there are new workitem revisions on server side
            CacheWorkItemRevision newSeverRev2 = CacheContext.ElementFactory.CreateRevision(2, "[New Tag Line 2]", "History");
            CacheWorkItemRevision newSeverRev3 = CacheContext.ElementFactory.CreateRevision(3, "[New Tag Line 3]", "History");
            CacheWorkItemRevision newSeverRev4 = CacheContext.ElementFactory.CreateRevision(4, "[New Tag Line 4]", "History");

            // emulate data driver: accept the changes (a new workitem on server side is available)
            CacheDataChanges changes = DataContext.ExportChanges();
            WorkItemBucket newBucket = new WorkItemBucket { Element = changes.ChangedWorkItems.First().Value.Element };
            WorkItemSavePair newSavePair = new WorkItemSavePair(newBucket)
            {
                State = updateState
            };

            changes.WorkItemsToSync[UserStory.UniqueId] = newSavePair;
            
            // the data driver knows all old revisions. these revisions must added as first elements
            changes.WorkItemsToSync[UserStory.UniqueId].Bucket.Element.Revisions.Add(revisions);

            // add new server revisions to the element
            changes.WorkItemsToSync[UserStory.UniqueId].Bucket.Element.Revisions.Add(newSeverRev2);
            changes.WorkItemsToSync[UserStory.UniqueId].Bucket.Element.Revisions.Add(newSeverRev3);
            changes.WorkItemsToSync[UserStory.UniqueId].Bucket.Element.Revisions.Add(newSeverRev4);
            changes.RemoveDirtyFlags();

            DataContext.ImportChanges(changes);

            // test event handling
            Assert.AreEqual(revisions.Count + 3, UserStory.Revisions.Count);
            Assert.IsNotNull(UserStory.Revisions.Where(rev => rev.Id == 2));
            Assert.IsNotNull(UserStory.Revisions.Where(rev => rev.Id == 3));
            Assert.IsNotNull(UserStory.Revisions.Where(rev => rev.Id == 4));
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}
